<template>
  <div class="my-index">
    <DesignerPage
      v-if="activeName === 'designer'"
      ref="designerPageRef"
      :disabled="disabled"
      :dict-codes="dics"
      :userId="userStore.userInfo.userId"
    ></DesignerPage>
    <CompanyPage
      v-if="activeName === 'company'"
      ref="companyPageRef"
      :userType="userType"
      :disabled="disabled"
      :dict-codes="dics"
      :companyId="userStore.userInfo.companyId"
    ></CompanyPage>
  </div>
</template>

<script setup>
import CommonAPI from "@/api/system/common";
import DesignerPage from "./designer/index.vue";
import CompanyPage from "./company/index.vue";
import DictDataAPI from "@/api/system/dict-data";
import { useDictStore } from "@/store/modules/dict";
import { useUserStore } from "@/store/modules/user";
import { storeGetUserType } from "@/utils/auth";
import { SYSTEM_TYPE_REVERSE_OBJECT, SYSTEM_INDEXPAGE_OBJECT } from "@/utils/enums/system";
import { findPranctitionerMerchantCompanyVO } from "@/utils/common";
import { UserTypeEnum } from "@/enums/system/UserEnum";
const userStore = useUserStore();
/**
 * 获取用户类型，看展示哪种主页
 */
const userType = ref();
// 如果是设计师和项目经理就展示设计师主页，否则展示公司主页
const isDesigner = ref(true);
const activeName = computed(() => (isDesigner.value ? "designer" : "company"));
const designerPageRef = ref(null);
const companyPageRef = ref(null);
const disabled = ref(true);

/**
 * 根据用户类型，来获取字典信息
 */
const dics = ref();
let fakeDics = ref([]);
const cateList = ref([]);
const dictStore = useDictStore();
async function getallDics() {
  fakeDics.value.forEach(async (item) => {
    try {
      // 如果字典没存就请求一下
      if (dictStore.getDictionary(item.dictCode).length === 0 && item.type == "tree") {
        const res = await DictDataAPI.getPage({
          dictCode: item.dictCode,
          pageNum: 1,
          pageSize: 1000,
        });
        dictStore.setDictionary({
          dictCode: item.dictCode,
          dictDataList: res.list,
        });
        if (item.dictCode == "materialCategory") {
          item.dictDataList = cateList.value;
        } else {
          item.dictDataList = res.list;
        }
      } else {
        item.dictDataList = dictStore.getDictionary(item.dictCode);
      }
    } catch (error) {
      console.error("获取字典表单失败", error);
    }
  });
}
/**
 * SYSTEM_TYPE_REVERSE_OBJECT["家居建材"]
 *  只有家居建材有品牌分类，其余都不获取
 */
const hasBrandFunc = async () => {
  try {
    // 如果品牌列表没有数据，则获取品牌列表
    const data = await CommonAPI.getBrandList({
      keyword: "",
    });

    fakeDics.value.push({
      id: "999",
      name: "品牌列表",
      dictCode: "BrandList",
      fieldName: "manageBrand",
      dictDataList: data.map((item) => ({
        label: item.brandName,
        value: item.id,
      })),
    });
  } catch (error) {
    console.error("获取品牌列表失败", error);
  }
};
async function getCateList() {
  try {
    const id = userStore.userInfo.userType == "MC02" ? 1 : 2;
    await CommonAPI.getCategoryInfo(id).then((res) => {
      cateList.value = res.data.map((item) => ({
        label: item.name,
        value: item.id.toString(),
        tagType: "info",
      }));
    });
  } catch (error) {
    console.error("获取分类失败", error);
  }
}

onMounted(async () => {
  await getCateList();
  let storeUserType = storeGetUserType();
  // 如果是从业人员，直接拿上级的公司type
  if (storeUserType === UserTypeEnum.PRACTITIONER.value) {
    storeUserType = findPranctitionerMerchantCompanyVO().merchantUserType;
  }
  userType.value = storeUserType;
  // 设置展示页面，设计师项目经理展示designer组件，其余都展示company组件
  isDesigner.value = [
    SYSTEM_TYPE_REVERSE_OBJECT["设计师"],
    SYSTEM_TYPE_REVERSE_OBJECT["项目经理"],
  ].includes(userType.value);
  // 不同页面配置不通的表单
  fakeDics.value = [...SYSTEM_INDEXPAGE_OBJECT[userType?.value]];
  dics.value = [];
  // 目前是如果有一个就说明有数据了
  await getallDics();
  // 只有家居建材有经营材料分类&&品牌，其余都不获取
  if (userType.value == SYSTEM_TYPE_REVERSE_OBJECT["家居建材"]) {
    // await hasBrandFunc();
  }
  dics.value = fakeDics.value;
});
</script>
<style lang="scss" scoped>
.my-index {
  height: 100%;
  display: flex;
  flex-direction: column;
  background-color: #fff;
  margin: 10px;
  padding: 10px 20px;
  height: calc(100% - 20px);
}
</style>
